//
//  MACTableView.m
//  MACTableView
//  https://github.com/azheng51714/MACTableView
//  Created by MacKun on 16/10/21.
//  Copyright © 2016年 MacKun All rights reserved.
//


#import "MACTableView.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>


@interface MACTableView()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>{
    
}

@end

@implementation MACTableView

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self=[super initWithCoder:aDecoder]) {
        [self initUI];
    }
    
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self =[super initWithFrame:frame style:style]) {
        [self initUI];
    }
    
    return self;
}
-(void)initUI{
    
    self.tableFooterView                = [UIView new];
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator   = NO;
    
    self.emptyTitle                     = @"暂无内容";
    self.emptySubtitle                  = @"";
    self.emptyAtrtibutedTitle           = nil;
    self.emptyAtrtibutedSubtitle        = nil;
    self.emptyImage                     = [UIImage imageNamed:@"common_nodata"];
    self.emptyColor                     = [UIColor whiteColor];
    self.showEmpty                      = YES;
    
    
}
#pragma  mark public methods

-(void)beginLoading{
    self.emptyDataSetDelegate = self;
    self.emptyDataSetSource   = self;
}

#pragma mark - EmptyDataSetSource Methods
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
    if (self.emptyAtrtibutedTitle) {
        return self.emptyAtrtibutedTitle;
    }
    NSString *text =self.emptyTitle;
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:15.0f],
                                 NSForegroundColorAttributeName: [UIColor lightGrayColor]};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
    if (self.emptyAtrtibutedSubtitle) {
        return self.emptyAtrtibutedSubtitle;
    }
    NSString *text = self.emptySubtitle;
    
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:15.0f],
                                 NSForegroundColorAttributeName: [UIColor lightGrayColor],
                                 NSParagraphStyleAttributeName: paragraph};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{
    return nil;
    //    NSString *text = nil;
    //    UIFont *font = nil;
    //    UIColor *textColor = nil;
    //    text = @"点击界面重新加载";
    //    font = [UIFont boldSystemFontOfSize:16.0];
    //    textColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
    //    NSMutableDictionary *attributes = [NSMutableDictionary new];
    //    if (font) [attributes setObject:font forKey:NSFontAttributeName];
    //    if (textColor) [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    //
    //    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
- (UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{
    return nil;
}
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    
    return self.emptyImage ? self.emptyImage:[UIImage imageNamed:@"common_nodata"];
}
- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView{
    return self.emptyColor;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView{
    return -64.0f;
}

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{
    return 0.0f;
}
#pragma mark - EmptyDataSetDelegate Methods

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView{
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView{
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}



- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"transform"];
    
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0)];
    
    animation.duration = 0.25;
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;
    
    return animation;
}


@end
